package com.example.expandlisttest;

import java.util.List;

import com.example.expandlisttest.MainActivity.groupNode;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class expandableListViewAdapter extends BaseExpandableListAdapter 
{
    Activity activity;
    List<groupNode> groupArray;
    List<List<String>> childArray;
    private OnClickListener l = null;
	public  expandableListViewAdapter(Activity a,List<groupNode> group_data,List<List<String>> child_data,OnClickListener btn_l)  
    {  
        activity = a;
        groupArray = group_data;
        childArray = child_data;
        l = btn_l;
    }  
   /*-----------------Child */
    @Override
    public Object getChild(int groupPosition, int childPosition) 
    {
        // TODO Auto-generated method stub
        return childArray.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) 
    {
        return buildChildView(groupPosition,childPosition);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childArray.get(groupPosition).size();
    }
   /* ----------------------------Group */
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return getGroup(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groupArray.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) 
    {
         
       
       return buildGroupView(groupPosition);
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) 
    {
        // TODO Auto-generated method stub
        return true;
    }
     
    private View  buildGroupView(int groupPosition) 
    {
    	View v = View.inflate(activity,R.layout.item_group_view, null);
    	groupNode g =groupArray.get(groupPosition);
    	TextView name = (TextView)v.findViewById(R.id.tv_catgroy_name);
    	ImageView expandIc = (ImageView)v.findViewById(R.id.iv_indcator);
    	
    	ImageView iv_edit= (ImageView)(ImageView)v.findViewById(R.id.iv_edit);
    	ImageView iv_more = (ImageView)v.findViewById(R.id.iv_more);
    	ImageView iv_arrow_down = (ImageView)v.findViewById(R.id.iv_arrow_down);
    	
    	iv_edit.setTag(groupPosition);
    	iv_more.setTag(groupPosition);
    	iv_arrow_down.setTag(groupPosition);
    	
    	iv_edit.setOnClickListener(l);
    	iv_more.setOnClickListener(l);
    	iv_arrow_down.setOnClickListener(l);
    	
    	LinearLayout group_l = (LinearLayout)v.findViewById(R.id.ll_group);
    	group_l.setBackgroundColor(g.mColor);
    	///build Extra
    	ImageView iv_extra_camera = (ImageView)g.mExtraView.findViewById(R.id.iv_gExtra_camera);
    	ImageView iv_extra_album = (ImageView)g.mExtraView.findViewById(R.id.iv_gExtra_album);
    	ImageView iv_extra_smile = (ImageView)g.mExtraView.findViewById(R.id.iv_gExtra_smile);
    	
    	iv_extra_camera.setTag(groupPosition);
    	iv_extra_album.setTag(groupPosition);
    	iv_extra_smile.setTag(groupPosition);
    	
    	iv_extra_camera.setOnClickListener(l);
    	iv_extra_album.setOnClickListener(l);
    	iv_extra_smile.setOnClickListener(l);
    	g.buildExtra();
    	
    	if(g.mIsExpand)
    	{
    		expandIc.setImageResource(R.drawable.zhan);
    	}else
    	{
    		expandIc.setImageResource(R.drawable.shou);
    	}
    	name.setText(g.mStr);
    	return v;
	}
    private View buildChildView(int groupPosition, int childPosition)
    {
    	String str =childArray.get(groupPosition).get(childPosition);
    	TextView v = new TextView(activity);
    	v.setText(str);
    	return v;
    	
    }
}
